package com.richinfo.stream.provider;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.annotation.Transformer;
import org.springframework.integration.core.MessageSource;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.GenericMessage;

/**
 * 消息生产者
 * Spring Integration原生支持
 * 
* Created by WangXJ
* 2018-11-27 10:59
*/
@EnableBinding(value = {SinkSender.SinkOutput.class})
public class SinkSender {

	private static Logger logger = LoggerFactory.getLogger(SinkSender.class);
	
	@Bean
	@InboundChannelAdapter(value = SinkOutput.OUTPUT, poller = @Poller(fixedDelay = "2000"))
	public MessageSource<Date> timerMessageSource(){
		MessageSource<Date> ms = () -> new GenericMessage<>(new Date());
		return ms;
	}
	
	@Transformer(inputChannel = Sink.INPUT,outputChannel = SinkOutput.OUTPUT)
	public Object transform(Date message) {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(message);
	}
	
	public interface SinkOutput{
		
		String OUTPUT = "output";
		
		@Output(SinkOutput.OUTPUT)
		MessageChannel output();
	}
}
